﻿using IdentityModel.Client;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Caching;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Identity;
using Volo.Abp.ObjectMapping;
using Volo.Abp.TenantManagement;
using IdentityUser = Volo.Abp.Identity.IdentityUser;

namespace Himp.Platform.Logins
{
    public class LoginService : PlatformAppService
    {
        private readonly IConfiguration _configuration;
        private LoginManager _loginManager;
        private readonly IMsgSendRecService _msgSendRecService;

        /// <summary>
        /// 
        /// </summary>
        protected IdentityUserManager UserManager { get; }

        /// <summary>
        /// 
        /// </summary>
        public SignInManager<IdentityUser> SignInManager { get; set; }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="httpClientFactory"></param>
        /// <param name="configuration"></param>
        /// <param name="userRepository"></param>
        /// <param name="userManager"></param>
        public LoginService(IConfiguration configuration
            , IdentityUserManager userManager
            , LoginManager loginManager
            , IMsgSendRecService msgSendRecService)
        {
            UserManager = userManager;
            _configuration = configuration;
            _loginManager = loginManager;
            _msgSendRecService = msgSendRecService;
        }

        /// <summary>
        /// 用户密码登录
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="UserLoginInvalidException"></exception>
        public async Task<AccountLoginDto> AccountAsync(AccountLoginInput input)
        {
            var accountLoginDto = new AccountLoginDto();
            var authServer = _configuration["AuthServer:Authority"] + "/connect/token";
            var accountLoginRes = await _loginManager.RequestId4(authServer, input.UserName, input.Password);
            var user = await UserManager.FindByNameAsync(input.UserName);
            var userDto = ObjectMapper.Map<IdentityUser, IdentityUserDto>(user);
            accountLoginDto.User = new AccountLoginUserDto(accountLoginRes.Item1, accountLoginRes.Item2, userDto);
            //var loginedExtendInfo = await _loginManager.GetLoginedExtendInfo(user.Id);
            //accountLoginDto.Staff = ObjectMapper.Map<PsnInfo, PsnInfoDto>(loginedExtendInfo.Item1);
            //accountLoginDto.Organ = ObjectMapper.Map<List<OrganInfo>, List<OrganInfoDto>>(loginedExtendInfo.Item2);

            //await _msgSendRecService.PostAsync(new MsgSendRecCreateDto()
            //{
            //    MsgBizTypeCodg = "00",
            //    MsgName = "系统消息",
            //    MsgCont = "登录成功",
            //    MsgLv = EnumMsgLv.Information
            //});

            return accountLoginDto;
        }
    }
}
